﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class MobileWebSite_MobileCheckout : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        decimal total = 0;

        OrderItemsMain oim = (OrderItemsMain)Session["orderitemsmain"];

        if (oim != null)
        {
            List<String> oil = oim.OrderItems;
            Session["order"] = new Order(getNextOrderId());

            foreach (String oi in oil)
            {
                LinkButton6.Text += oi + "<br />";

                total += getCost(oi);

                addToOrder(oi);
            }

            LinkButton6.Text += "Total " + total.ToString("C2") + "<br />";
        }

    }

    private void addToOrder(String item)
    {
        string strConnection = "Data Source=csd.columbusstate.edu;Initial Catalog=6118_fa12_TeamC;User ID=student;Password=cscpsc12";

        SqlConnection connection = new SqlConnection(strConnection);

        String query = "SELECT PID FROM PRODUCTS WHERE DESCRIP = '" + item + "'";

        SqlCommand command = new SqlCommand(query, connection);

        SqlDataReader reader;

        connection.Open();

        reader = command.ExecuteReader();

        reader.Read();

        int i = Convert.ToInt32(reader["PID"].ToString());

        reader.Close();

        Order o = (Order)Session["order"];

        o.addOrderItem(i, 1, -1);
    }

    private int getNextOrderId()
    {
        int oid = -1;
        SqlConnection conn = new SqlConnection("Data Source=csd.columbusstate.edu;Initial Catalog=6118_fa12_TeamC;Persist Security Info=True;User ID=unaeze_christiana;Password=sid2245");
        conn.Open(); 

        SqlCommand cmd = new SqlCommand("GetNextOID", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlParameter output = new SqlParameter("@orderid", SqlDbType.Int);
        output.Direction = ParameterDirection.Output;
        cmd.Parameters.Add(output);
        cmd.ExecuteNonQuery();

        oid =  Convert.ToInt32(output.Value.ToString());
        conn.Close();
        return oid;

    }

    private decimal getCost(String item)
    {

        string strConnection = "Data Source=csd.columbusstate.edu;Initial Catalog=6118_fa12_TeamC;User ID=student;Password=cscpsc12";

        SqlConnection connection = new SqlConnection(strConnection);

        String query = "SELECT PRICE FROM PRODUCTS WHERE DESCRIP = '" + item + "'";

        SqlCommand command = new SqlCommand(query, connection);

        SqlDataReader reader;

        connection.Open();

        reader = command.ExecuteReader();

        reader.Read();

        decimal d = Convert.ToDecimal(reader["PRICE"].ToString());

        reader.Close();

        return d;
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session["orderitemsmain"] = null;
        Session["order"] = null;

        Response.Redirect("Default.aspx");
    }
    protected void LinkButton6_Click(object sender, EventArgs e)
    {

    }
    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        if (Session["customer"] == null)
            Response.Redirect("MobileLogin.aspx");
        else
            Response.Redirect("MobileComplete.aspx");
    }
}
